package cn.edu.cqytxy.dao;

import cn.edu.cqytxy.entity.User;
import cn.edu.cqytxy.util.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 用户dao
 *
 * @author yinjw
 */
public class UserDao {

    public User queryByUserNo(String userNo) {
        // 获取数据连接
        Connection connection = DBUtils.getConn();

        String sql = "select * from user where user_no='" + userNo + "'";
        User user = null;
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserNo(rs.getString("user_no"));
                user.setUserName(rs.getString("user_name"));
                user.setUserPwd(rs.getString("user_pwd"));
            }
        } catch (Exception e) {
            System.out.println("查询出错");
            e.printStackTrace();
        }
        return user;
    }

    public static void main(String[] args) {
        UserDao userDao = new UserDao();
        User user = userDao.queryByUserNo("root");
        System.out.println(user.getUserName());
    }
}
